package com.trkj.base;

import android.app.ActionBar;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;
import com.trkj.widget.xiaomi.SystemBarTintManager;
import com.trkj.widget.xiaomi.XiaomiTint;

/**
 * Created by LiZanhong on 2015/06/15.
 */
public class BaseFragmentActivity extends FragmentActivity {
    protected SystemBarTintManager mTintManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initWindow();
        initActionBar();

        AppManager.getAppManager().addActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserService.getInstance(this).isLogin();
    }

    protected void initWindow(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            XiaomiTint.setTranslucentStatus(true, this);
            mTintManager = new SystemBarTintManager(this);
            if("Xiaomi".equals(Build.MANUFACTURER)) {
                XiaomiTint.setStatusBarDarkMode(false, this);
            }
            mTintManager.setStatusBarTintEnabled(true);
            mTintManager.setStatusBarTintResource(R.color.orange);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    protected void initActionBar(){
        ActionBar actionBar = this.getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);

        View subTitle = createCenterSubTitile(getCenterTitle());
        actionBar.setCustomView(subTitle, new
                ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT));
        ActionBar.LayoutParams mP = (ActionBar.LayoutParams) subTitle.getLayoutParams();
        mP.gravity = mP.gravity & ~Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.CENTER_HORIZONTAL;
        actionBar.setCustomView(subTitle, mP);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        }else{
        }
    }

    protected View createCenterSubTitile(String title){
        TextView subTitle = new TextView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        subTitle.setLayoutParams(lp);
        subTitle.setText(title);
        setSubTitleDecoration(subTitle);
        return subTitle;
    }

    protected void setSubTitleDecoration(TextView textView){
        textView.setBackgroundResource(android.R.color.transparent);
        textView.setTextColor(Constants.Color.getColor(this, android.R.color.white));
        textView.setTextSize(16);
    }

    protected String getCenterTitle(){

        return "";
    };

    protected void toast(String content){
        Toast.makeText(this, content, Toast.LENGTH_LONG).show();
    }


}
